package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@Service
public class IItemServiceImpl implements IItemService {

    @Autowired
    RemoteProductService remoteProductService;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Override
    public ItemVo item(Long skuId) {

        ItemVo itemVo = new ItemVo();

        // sku
        CompletableFuture<ProductSku> skuCompletableFuture = CompletableFuture.supplyAsync(new Supplier<ProductSku>() {
            @Override
            public ProductSku get() {
                R<ProductSku> productSkuR = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);// 内部接口header参数
                ProductSku sku = productSkuR.getData();// 线程1
                itemVo.setProductSku(sku); // sku
                return sku;
            }
        }, threadPoolExecutor);


        // product=spu
        CompletableFuture<Product> spuCompletableFuture = skuCompletableFuture.thenApplyAsync(new Function<ProductSku, Product>() {
            @Override
            public Product apply(ProductSku productSku) {
                Long productId = productSku.getProductId();
                Product product = remoteProductService.getProduct(productId, SecurityConstants.INNER).getData();// 线程2
                itemVo.setProduct(product);// spu
                itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));// 规格
                return product;
            }
        }, threadPoolExecutor);

        // 库存
        CompletableFuture<Void> stockCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                R<SkuStockVo> skuStock = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);// 线程3
                SkuStockVo skuStockVo = skuStock.getData();
                itemVo.setSkuStockVo(skuStockVo); // 库存
            }
        }, threadPoolExecutor);


        // 价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                SkuPrice skuPrice = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER).getData();// 线程4
                itemVo.setSkuPrice(skuPrice);// price
            }
        }, threadPoolExecutor);

        // 规格

        CompletableFuture<Void> mapCompletableFuture = spuCompletableFuture.thenAcceptAsync(new Consumer<Product>() {
            @Override
            public void accept(Product product) {
                Long productId = product.getId();
                Map<String, Long> skuSpecValueMap = remoteProductService.getSkuSpecValue(productId, SecurityConstants.INNER).getData();// 线程5
                itemVo.setSkuSpecValueMap(skuSpecValueMap);// 规格:skuId的map
            }
        }, threadPoolExecutor);

        // 轮播图和海报

        CompletableFuture<Void> detailsCompletableFuture = spuCompletableFuture.thenAcceptAsync(new Consumer<Product>() {
            @Override
            public void accept(Product product) {
                String sliderUrls = product.getSliderUrls();
                Long productId = product.getId();
                // 海报
                ProductDetails productDetails = remoteProductService.getProductDetails(productId, SecurityConstants.INNER).getData();// 线程6
                itemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));// 轮播图
                itemVo.setDetailsimagesUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));// 海报
            }
        }, threadPoolExecutor);

        CompletableFuture.allOf(skuCompletableFuture,spuCompletableFuture,stockCompletableFuture, priceCompletableFuture, mapCompletableFuture, detailsCompletableFuture).join();

        return itemVo;
    }

    public ItemVo itemBak(Long skuId) {

        // sku
        R<ProductSku> productSkuR = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);// 内部接口header参数
        ProductSku sku = productSkuR.getData();// 线程1
        // product=spu
        Long productId = sku.getProductId();
        Product product = remoteProductService.getProduct(productId, SecurityConstants.INNER).getData();// 线程2
        // 库存
        R<SkuStockVo> skuStock = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);// 线程3
        SkuStockVo skuStockVo = skuStock.getData();
        // 价格
        SkuPrice skuPrice = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER).getData();// 线程4
        // 规格
        Map<String, Long> skuSpecValueMap = remoteProductService.getSkuSpecValue(productId, SecurityConstants.INNER).getData();// 线程5
        // 轮播图
        String sliderUrls = product.getSliderUrls();
        // 海报
        ProductDetails productDetails = remoteProductService.getProductDetails(productId, SecurityConstants.INNER).getData();// 线程6

        ItemVo itemVo = new ItemVo();
        itemVo.setProductSku(sku); // sku
        itemVo.setSkuStockVo(skuStockVo); // 库存
        itemVo.setSkuPrice(skuPrice);// price
        itemVo.setProduct(product);// spu
        itemVo.setSkuSpecValueMap(skuSpecValueMap);// 规格:skuId的map
        itemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));// 轮播图
        itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));// 规格
        itemVo.setDetailsimagesUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));// 海报
        return itemVo;
    }
}
